package com.example.fitapp.activity.activity;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.fitapp.activity.R;
import com.example.fitapp.activity.bean.BodyDataInfo;
import com.example.fitapp.activity.bean.BodyDataList;
import com.example.fitapp.activity.bean.MySerializable;
import com.example.fitapp.activity.chart.MyChart;
import com.github.mikephil.charting.charts.LineChart;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BodyDataActivity extends AppCompatActivity {
    private int lineColor = 0xFF00ce8c;
    private int index = 0;
    LineChart mLineChart;
    private  List<TextView> textViewList = new ArrayList();
    private List<BodyDataList> bodyDataLists = new ArrayList<>();
    private MyChart mCHart;
    private TextView limitTitle;
    private TextView limitValue;
    private LinearLayout setLimit;
    private  BodyDataList mLBodyist ;
    private MySerializable mySerializable;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_body_data);
        initView();

        BodyDataInfo b1 = new BodyDataInfo();
        b1.setTradeDate("20191202");
        b1.setValue(172F);
        bodyDataLists.get(0).getBodyData().add(b1);
//        bodyDataLists.get(1).getBodyData().add(b1);
//        bodyDataLists.get(2).getBodyData().add(b1);
        mCHart = new MyChart(mLineChart);
        mLBodyist = bodyDataLists.get(0);
        setLine(mLBodyist);


//        List<BodyDataInfo> list = new ArrayList<>();
//        list.add(b1);


        Button add_data_btn = (Button)  findViewById(R.id.add_data);
        add_data_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                        BodyDataInfo b2 = new BodyDataInfo();
//                        b2.setTradeDate("20191203");
//                        b2.setValue(182F);

                Intent limitIntent = new Intent(BodyDataActivity.this,EditInputActivity.class);
                limitIntent.putExtra("extra_title","添加数据");
                startActivityForResult(limitIntent,2);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String returnedData;
        switch (requestCode) {
            case 1:
                if(resultCode == RESULT_OK){
                    returnedData = data.getStringExtra("data_return");
                    float value = Float.parseFloat(returnedData);

                    limitValue.setText(value>0?value+"":"暂未设定");

                    mCHart.setLimt(value);
                    mLBodyist.setLimitValue(value);
                }
                break;
            case 2:
                if(resultCode == RESULT_OK){
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
                String format = simpleDateFormat.format(new Date()).replace("-", "");
                 returnedData = data.getStringExtra("data_return");
                //添加数据
                BodyDataInfo b = new BodyDataInfo();
                b.setTradeDate(format);
                b.setValue(Float.parseFloat(returnedData));

                mLBodyist.getBodyData().add(b);
                setLine(mLBodyist);
                }
                break;
            default:
                break;
        }
    }

    private void setLine(BodyDataList bodyDataList){
        mCHart.initChart() ;
        mCHart.showLineChart(bodyDataList.getBodyData(), "我的"+bodyDataList.getTitle(), lineColor);
        if(bodyDataList.getLimitValue()>0){
                mCHart.setLimt(bodyDataList.getLimitValue());
        }
        limitTitle.setText("目标"+ bodyDataList.getTitle());
        float value = bodyDataList.getLimitValue();
        limitValue.setText(value>0?value+"":"暂未设定");
    }

    private void initView(){

        textViewList.add(findViewById(R.id.nav1));
        textViewList.add(findViewById(R.id.nav2));
        textViewList.add(findViewById(R.id.nav3));

        bodyDataLists.add(new BodyDataList("身高",0));
        bodyDataLists.add(new BodyDataList("体重",0));
        bodyDataLists.add(new BodyDataList("胸围",0));

        mLineChart = findViewById(R.id.mLineChart);
        limitTitle= findViewById(R.id.limit_title);
        limitValue = findViewById(R.id.limit_value);
        setLimit = findViewById(R.id.set_limit);
        setLimit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent limitIntent = new Intent(BodyDataActivity.this,EditInputActivity.class);
                limitIntent.putExtra("extra_title","更改目标值");
                limitIntent.putExtra("extra_value",String.valueOf(mLBodyist.getLimitValue()));
                startActivityForResult(limitIntent,1);


            }
        });
        for(int i = 0;i< textViewList.size();i++){
            int finalI = i;
            textViewList.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(finalI!=index){
                        mLBodyist = bodyDataLists.get(finalI);
                        setLine(bodyDataLists.get(finalI));
                    }
                    index = finalI;
                    for(int j = 0;j< textViewList.size();j++){
                        if(j!=finalI){
                            textViewList.get(j).setTextColor(Color.parseColor("#888888"));
                        }else{
                            textViewList.get(j).setTextColor(Color.parseColor("#ffffff"));
                        }
                    }
                }
            });
        }
    }
}
